<script setup>
	import adminApi from "@/api/admin.js";
	import {reactive, ref} from 'vue'
	import {ElMessage, ElMessageBox} from "element-plus";
	import {Plus} from '@element-plus/icons-vue'

	//表格数据
	const list = ref([])
	const total = ref(0)
	//分页信息和搜索条件
	const adminQuery = reactive({
		name: '',
		phone: '',
		page: 1,
		limit: 10
	})

	// function loadData() {
	// 	adminApi.list(adminQuery).then(result =>{
	// 		if(result.code == 0){
	// 			list.value = result.data.records
	// 			total.value = result.data.total
	// 		}
	// 	})
	// }


	const loadData = () => {
		adminApi.list(adminQuery).then(result => {
			if (result.code == 0) {
				list.value = result.data.records
				total.value = result.data.total
			}
		})
	}
	loadData()

	//根据id删除
	const deleteById = (id) => {
		ElMessageBox.confirm(
			'您确认要删除么?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false //防止抖动
			}
		).then(() => {
			adminApi.deleteById(id).then(result => {
				if (result.code == 0) {
					ElMessage.success(result.msg)
					loadData()
				} else {
					ElMessage.error(result.msg)
				}
			})
		})
	}

	//批量删除
	const ids = ref([])
	const handleSelectionChange = (rows) => {
		console.log('多选：' + rows)
		ids.value = rows.map(row => row.id)
		console.log(ids)
	}

	const deleteAll = () => {
		ElMessageBox.confirm(
			'您确认要删除么?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false //防止抖动
			}
		).then(() => {
			adminApi.deleteAll(ids.value).then(result => {
				if (result.code == 0) {
					ElMessage.success(result.msg)
					loadData()
				} else {
					ElMessage.error(result.msg)
				}
			})
		})
	}

	//添加，编辑
	const admin = ref({})
	const dialogFormVisible = ref(false)
	const title = ref()
	const showAddDialog = () => {
		dialogFormVisible.value = true
		admin.value = {}
		title.value = '添加'
	}

	const showUpdateDialog = (id) => {
		dialogFormVisible.value = true
		admin.value = {}
		title.value = '编辑'
		adminApi.selectById(id).then(result => {
			if (result.code == 0) {
				admin.value = result.data
			}
		})
	}

	const addOrUpdate = () => {
		if (admin.value.id) {//编辑
			adminApi.update(admin.value).then(result => {
				if (result.code == 0) {
					ElMessage.success(result.msg)
					dialogFormVisible.value = false
					loadData()
				} else {
					ElMessage.error(result.msg)
				}
			})
		} else {
			adminApi.add(admin.value).then(result => {
				if (result.code == 0) {
					ElMessage.success(result.msg)
					dialogFormVisible.value = false
					loadData()
				} else {
					ElMessage.error(result.msg)
				}
			})
		}
	}

	//图片上传
	const handleAvatarSuccess = (result) => {
		admin.value.avatar = result.data
	}

	//switch
	const handleSwitchChange = (row) => {
		const admin = {}
		admin.id = row.id
		admin.status = row.status
		adminApi.update(admin).then(result => {
			if (result.code == 0) {
				ElMessage.success(result.msg)
			} else {
				ElMessage.error(result.msg)
			}
		})
	}

</script>

<template>
	<el-card>
		<template #header>
			<div class="header">
				<el-button type="primary" @click="showAddDialog">添加</el-button>
				<el-button type="primary" @click="deleteAll">批量删除</el-button>
			</div>
		</template>

		<el-form :inline="true">
			<el-form-item label="名字">
				<el-input v-model="adminQuery.name" placeholder="请输入名字" clearable/>
			</el-form-item>

			<el-form-item label="电话">
				<el-input v-model="adminQuery.phone" placeholder="请输入电话" clearable/>
			</el-form-item>

			<el-form-item label="更新时间" :label-width="100">
				<el-date-picker
					v-model="adminQuery.beginUpdateTime"
					type="datetime"
					value-format="YYYY-MM-DD HH:mm:ss"
					placeholder="开始时间"
				/>
				<el-date-picker
					v-model="adminQuery.endUpdateTime"
					type="datetime"
					value-format="YYYY-MM-DD HH:mm:ss"
					placeholder="结束时间"
				/>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" @click="loadData">搜索</el-button>
			</el-form-item>
		</el-form>

		<el-table :data="list" style="width: 100%" ref="multipleTableRef" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55"/>
			<el-table-column fixed prop="id" label="ID"/>
			<el-table-column prop="name" label="名字"/>
			<el-table-column prop="password" label="密码"/>
			<el-table-column prop="phone" label="电话"/>
			<el-table-column prop="email" label="邮箱"/>
<!--			<el-table-column prop="avatar" label="头像"/>-->
			<el-table-column prop="avatar" label="头像">
				<template #default="scope">
					<img :src="scope.row.avatar" style="max-height: 40px; max-width: 120px;"/>
				</template>
			</el-table-column>

			<el-table-column prop="status" label="状态">
				<template #default="scope">
					<el-switch
						v-model="scope.row.status"
						style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
						:active-value="1"
						:inactive-value="0"
						@change="handleSwitchChange(scope.row)"
					/>
				</template>
			</el-table-column>

			<el-table-column prop="updateTime" label="更新时间"/>

			<el-table-column fixed="right" label="Operations">
				<template #default="{ row }">
					<el-button type="primary" @click="showUpdateDialog(row.id)">编辑</el-button>
					<el-button type="danger" @click="deleteById(row.id)">删除</el-button>
				</template>
			</el-table-column>

		</el-table>

		<el-pagination
			v-model:current-page="adminQuery.page"
			v-model:page-size="adminQuery.limit"
			:page-sizes="[10, 20, 30, 40]"
			layout="total, sizes, prev, pager, next, jumper"
			:total="total"
			@change="loadData"
			style="margin-top: 20px; justify-content: flex-end"
		/>
	</el-card>

	<!--添加、编辑弹出框-->
	<el-dialog v-model="dialogFormVisible" :title="title" width="500" :lock-scroll="false">
		<el-form :model="admin">
			<el-form-item label="名字" :label-width="60">
				<el-input v-model="admin.name" autocomplete="off"/>
			</el-form-item>

			<el-form-item label="密码" :label-width="60">
				<el-input v-model="admin.password" autocomplete="off"/>
			</el-form-item>

			<el-form-item label="邮箱" :label-width="60">
				<el-input v-model="admin.email" autocomplete="off"/>
			</el-form-item>

			<el-form-item label="手机号" :label-width="60">
				<el-input v-model="admin.phone" autocomplete="off"/>
			</el-form-item>

			<el-form-item label="头像" :label-width="60">
				<el-upload
					class="avatar-uploader"
					action="/api/upload"
					:show-file-list="false"
					:on-success="handleAvatarSuccess">
					<img v-if="admin.avatar" :src="admin.avatar" class="avatar"/>
					<el-icon v-else class="avatar-uploader-icon">
						<Plus/>
					</el-icon>
				</el-upload>
			</el-form-item>
		</el-form>

		<template #footer>
			<div class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取消</el-button>
				<el-button type="primary" @click="addOrUpdate">
					确认
				</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<style scoped>
.avatar-uploader .avatar {
	width: 178px;
	height: 178px;
	display: block;
}

.avatar-uploader .el-upload {
	border: 1px dashed var(--el-border-color);
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
	transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
	border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	text-align: center;
}
</style>